﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for DanhSachKhachHang.xaml
    /// </summary>
    public partial class DanhSachKhachHang : Window, IPhanQuyenHelper
    {
        public DanhSachKhachHang()
        {
            InitializeComponent();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        private void Refresh()
        {
            txtMaKhachHang.Text = KhachHangDAO.GetLastMaKhachHang().ToString();
            txtTenKhachHang.Text = "";
            txtTienNoHienTai.Text = "";
            txtDiaChi.Text = "";
            cbLoaiKhachHang.SelectedIndex = 0;
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
            List<KHACHHANG> dt = KhachHangDAO.GetAllKhachHang();
            dgDanhSachKhachHang.ItemsSource = dt;

            List<String> dtl = KhachHangDAO.GetAllTenLoaiKhachHang();
            cbLoaiKhachHang.ItemsSource = dtl;
            CurrentUser.Instance.AddForm(this);

            this.Refresh();
        }

        private void dgDanhSachKhachHang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = dgDanhSachKhachHang.SelectedIndex;
            List<KHACHHANG> lstKhach = KhachHangDAO.GetAllKhachHang();
            List<LOAIKHACHHANG> lstLoai = KhachHangDAO.GetAllLoaiKhachHang();
            if (index < 0 || index >= lstKhach.Count)
                return;
            //lstKhach[index].IsChecked = !lstKhach[index].IsChecked;

            dgDanhSachKhachHang.ItemsSource = lstKhach;
            //if (lstKhach[index].IsChecked)
            {
                txtMaKhachHang.Text = lstKhach[index].MaKhachHang.ToString();
                txtTenKhachHang.Text = lstKhach[index].TenKhachHang;
                txtDiaChi.Text = lstKhach[index].DiaChi;
                txtTienNoHienTai.Text = lstKhach[index].TienNoHienTai.ToString();
                cbLoaiKhachHang.SelectedIndex = lstLoai.IndexOf(lstKhach[index].LOAIKHACHHANG);
            }

            btnThem.IsEnabled = false;
            btnCapNhat.IsEnabled = true;
        }

        private void dgDanhSachKhachHang_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = dgDanhSachKhachHang.SelectedIndex;
            List<KHACHHANG> lstKhach = KhachHangDAO.GetAllKhachHang();
            List<LOAIKHACHHANG> lstLoai = KhachHangDAO.GetAllLoaiKhachHang();
            if (index < 0 || index >= lstKhach.Count)
                return;
            //lstKhach[index].IsChecked = !lstKhach[index].IsChecked;

            dgDanhSachKhachHang.ItemsSource = lstKhach;
            //if (lstKhach[index].IsChecked)
            {
                txtMaKhachHang.Text = lstKhach[index].MaKhachHang.ToString();
                txtTenKhachHang.Text = lstKhach[index].TenKhachHang;
                txtDiaChi.Text = lstKhach[index].DiaChi;
                txtTienNoHienTai.Text = lstKhach[index].TienNoHienTai.ToString();
                cbLoaiKhachHang.SelectedIndex = lstLoai.IndexOf(lstKhach[index].LOAIKHACHHANG);
            }

            btnThem.IsEnabled = false;
            btnCapNhat.IsEnabled = true;
        }

        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            if (txtTenKhachHang.Text == "")
            {
                MessageBox.Show("Chưa nhập tên khách hàng.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenKhachHang.Focus();
            }
            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Chưa nhập địa chỉ khách hàng.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtDiaChi.Focus();
            }
            else
            {
                List<LOAIKHACHHANG> lstLoaiKhach = KhachHangDAO.GetAllLoaiKhachHang();
                KHACHHANG kh = new KHACHHANG
                {
                    TenKhachHang = txtTenKhachHang.Text,
                    DiaChi = txtDiaChi.Text,
                    MaLoaiKhachHang = lstLoaiKhach[cbLoaiKhachHang.SelectedIndex].MaLoaiKhachHang,
                    TienNoHienTai = 0
                };
                if (KhachHangBUS.ThemKhachHang(kh))
                {
                    Window_Loaded(sender, e);
                    this.Refresh();
                    MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            List<KHACHHANG> lstKhachHang = KhachHangDAO.GetAllKhachHang();
            bool isSuccessful = false;

            foreach (KHACHHANG item in lstKhachHang)
            {
                if (item.IsChecked)
                {
                    isSuccessful = true;
                }
            }

            if (isSuccessful == false)
                return;

            MessageBoxResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa các trường đã chọn?", "Xác nhận xóa", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return;
            }

            
            foreach (KHACHHANG item in lstKhachHang)
            {
                if (item.IsChecked)
                {
                    isSuccessful = true;
                    if (KhachHangBUS.XoaKhachHang(item))
                    {

                        //MessageBox.Show("Xóa thành công");
                    }
                    else
                    {
                        MessageBox.Show("Xoá thất bại.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }
            }
            Window_Loaded(sender, e);
            Refresh();
            MessageBox.Show("Xóa thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            Refresh();
        }

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaKhachHang.Text == "")
                return;

            if (txtTenKhachHang.Text == "")
            {
                MessageBox.Show("Chưa nhập tên khách hàng.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenKhachHang.Focus();
                return;
            }
            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Chưa nhập địa chỉ khách hàng.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtDiaChi.Focus();
                return;
            }

            List<LOAIKHACHHANG> lstLoaiKhach = KhachHangDAO.GetAllLoaiKhachHang();
            KHACHHANG kh = new KHACHHANG();
            kh.MaKhachHang = int.Parse(txtMaKhachHang.Text);
            kh.TenKhachHang = txtTenKhachHang.Text;
            kh.TienNoHienTai = decimal.Parse(txtTienNoHienTai.Text);
            kh.DiaChi = txtDiaChi.Text;
            kh.MaLoaiKhachHang = lstLoaiKhach[cbLoaiKhachHang.SelectedIndex].MaLoaiKhachHang;
            if (KhachHangBUS.CapNhatKhachHang(kh))
            {
                Window_Loaded(sender, e);
                Refresh();
                MessageBox.Show("Cập nhật thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("Cập nhật thất bại.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            TraCuuKhachHang fmTraCuu = new TraCuuKhachHang();
            fmTraCuu.Show();
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
            btnThem.IsEnabled = true;
            //btnThemMoi.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            btnThem.IsEnabled = true;
            //btnThemMoi.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            btnThem.IsEnabled = false;
           //btnThemMoi.IsEnabled = false;
            btnCapNhat.IsEnabled = false;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }

        private void txtTenKhachHang_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Space && e.Key != Key.Back && e.Key != Key.OemQuotes && e.Key != Key.Tab && ((e.Key < Key.A) || (e.Key > Key.Z)))
                e.Handled = true;
        }
    }
}
